package topofstack.networkserverutils;

import topofstack.networkserverutils.bean.Location;
import topofstack.networkserverutils.bean.SiteInfo;
import topofstack.networkserverutils.net.GeoLocation;
import topofstack.networkserverutils.net.Nslookup;
import topofstack.networkserverutils.net.Whois;
import topofstack.networkserverutils.util.Constants;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MailServerInfo extends Activity {
	
	public static final String SERVER_NAME = "name";
	
	private SiteInfo siteInfo;
	private String whoisInfo;
	
	private String serverName;
	
	private TextView aRecord;
	private TextView whois;
	private Button showMap;
	
	private ProgressDialog dialog;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.server_info);
		
		serverName = getIntent().getExtras().getString(SERVER_NAME);
		
		aRecord = (TextView) findViewById(R.id.server_A_record);
		whois = (TextView) findViewById(R.id.server_whois);
		showMap = (Button) findViewById(R.id.show_map);
		
		showMap.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View arg0) {
				new RetriveLocation().execute(new Void [0]);
				showProgressDialog();
			}
		});
		
		new RetriveData().execute(new Void[0]);
		showProgressDialog();
	}
	
	private void showProgressDialog() {
		dialog = new ProgressDialog(this);
		dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		dialog.setMessage(Constants.DIALOG_MESSAGE);
		dialog.setIndeterminate(true);
		dialog.show();
	}
	
	private class RetriveData extends AsyncTask<Void, Void, Void> {
		
		@Override
		protected Void doInBackground(Void... params) {
			
			siteInfo = Nslookup.getA(serverName);
			
			if(siteInfo != null) {
				whoisInfo = Whois.get(siteInfo.ipAddress, true);
			}
			
			return null;
		}
		
		@Override
		protected void onPostExecute(Void v) {
			
			if(siteInfo == null || whoisInfo == null)
				Toast.makeText(getBaseContext(), Constants.ERROR_MESSAGE, 
						Toast.LENGTH_SHORT).show();
			else {
				aRecord.setText(siteInfo.toString());
				whois.setText(whoisInfo);
			}
			
			dialog.cancel();
		}
	}
	
private class RetriveLocation extends AsyncTask<Void, Void, Location> {
		
		@Override
		protected Location doInBackground(Void... params) {
			if(siteInfo == null)
				return null;
			Location location = GeoLocation.get(siteInfo.name);
			
			return location;
		}
		
		@Override
		protected void onPostExecute(Location location) {
			
			if(location == null) {
				if(siteInfo == null)
					Toast.makeText(getBaseContext(), Constants.ERROR_MESSAGE, Toast.LENGTH_SHORT)
					.show();
				else
					Toast.makeText(getBaseContext(), siteInfo.name + " has no LOC record",
							Toast.LENGTH_SHORT).show();
			}
			else {
					Intent intent = new Intent(getBaseContext(), Map.class);
					intent.putExtra(Map.KEY_LAT, location.getLatitude());
					intent.putExtra(Map.KEY_LON, location.getLongitude());
					startActivity(intent);
				
				Log.d("Location", location.getLatitude() + " " + location.getLongitude());
			}
			dialog.cancel();
		}
	}
}
